package net.ptcs.litang.dao;

import net.ptcs.litang.util.DBUtils_pool;
import net.ptcs.litang.util.ResultSetUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 票数据操作
 *
 * @author 赵浩森
 * @version 1.0
 * @since 2024/6/16 15:23
 */
public class OrderDao {
    public String QUERY_Order_BY_USERID = "SELECT oltc.city         AS ocity,\n" +
            "       dltc.city         AS dcity,\n" +
            "       lts.start_time    AS starttime,\n" +
            "       lts.end_time      AS endtime,\n" +
            "       lto.order_time    AS ordertime\n" +
            "FROM litang_ticket_order lto\n" +
            "         LEFT JOIN litang_ticket_stock lts\n" +
            "                   ON lto.stock_id = lts.id\n" +
            "         LEFT JOIN litang_tab_city oltc\n" +
            "                   ON lts.origin_city_id = oltc.id\n" +
            "         LEFT JOIN litang_tab_city dltc\n" +
            "                   ON lts.destination_city_id = dltc.id\n" +
            "WHERE user_id = ? ";

    public JSONArray selectOrderAll(Integer userId) throws ClassNotFoundException, SQLException {

        JSONArray dataContent = null;

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet resultSet = null;
        try {
            conn = DBUtils_pool.getConnection();
            stmt = conn.prepareStatement(QUERY_Order_BY_USERID);
            stmt.setInt(1, userId);
            try {
                dataContent = ResultSetUtil.resultSetToJson(stmt.executeQuery());
            } catch (SQLException | JSONException e) {
                throw new RuntimeException(e);
            }
            return dataContent;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils_pool.release(conn, stmt, resultSet);
        }
    }
}